﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Program.cs" company="3po.ru">
//   Author: Alexander Ulitin [support@3po.ru]
//   
//   This file is part of Conusic.
//   
//   Conusic is free software: you can redistribute it and/or modify
//   it under the terms of the GNU General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
//   
//   Conusic is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU General Public License for more details.
//   
//   You should have received a copy of the GNU General Public License
//   along with Conusic.  If not, see http://www.gnu.org/licenses/.
// </copyright>
// <summary>
//   Class with program entry point.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

#define EnableAll
namespace ConusicUI
{
    using System;
    using System.Net;
    using System.Windows.Forms;

    using Conusic;
    using Conusic.Properties;

    /// <summary>
    /// Class with program entry point.
    /// </summary>
    internal static class Program
    {
        #region Static Fields

        /// <summary>
        /// The internet connection window.
        /// </summary>
        private static ConnectionTester internetConnectionWindow;

        #endregion

        #region Methods

        /// <summary>
        /// Program entry point.
        /// </summary>
        [STAThread]
        private static void Main( /*string[] args*/)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            if (Conusic.Properties.Settings.Default.FirstRun)
            {
                Conusic.Properties.Settings.Default.Upgrade();
            }

            // Subscribe to event error handler in http requests
            SimpleHttp.OnError += SimpleHttp_OnError;

            // Save settings on exit
            AppDomain.CurrentDomain.ProcessExit += (sender, args) => Settings.Default.Save();

#if EnableAll

#if DEBUG4
            var s = new TestForm();
            s.ShowDialog();
            Environment.Exit(0);
#endif

            // Safe program from http 417 error - do not send except header in webrequest
            // http://blogs.msdn.com/b/shitals/archive/2008/12/27/9254245.aspx
            ServicePointManager.Expect100Continue = false;

            AutoUpdate.CheckUpdateIfEnabled();

            bool successAuthorization = false;

            successAuthorization = RunAuthorization();

            if (successAuthorization)
            {
                RunApplication();
            }
#endif
        }

        /// <summary>
        /// The run application.
        /// </summary>
        private static void RunApplication()
        {
            Application.Run(new Main());
            Properties.Settings.Default.Save();
        }

        /// <summary>
        /// The run authorization.
        /// </summary>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        private static bool RunAuthorization()
        {
            if (!string.IsNullOrEmpty(Settings.Default.SID)
                && VkontakteAuthorization.CheckAuthorization(Settings.Default.SID))
            {
                return true;
            }

            var authForm = new FormAuth();
            if (authForm.ShowDialog() == DialogResult.OK)
            {
                return VkontakteAuthorization.CheckAuthorization(Settings.Default.SID);
            }

            return false;
        }

        /// <summary>
        /// The simple http_ on error.
        /// </summary>
        /// <param name="e">
        /// The e.
        /// </param>
        private static void SimpleHttp_OnError(Exception e)
        {
            Console.Error.Write(e);
            Console.Error.Write(e.StackTrace);
            // Ignore if another window configuration open
            if (internetConnectionWindow == null)
            {
                internetConnectionWindow = new ConnectionTester(e);
                if (Form.ActiveForm != null)
                {
                    Form.ActiveForm.Invoke(
                        new MethodInvoker(delegate { internetConnectionWindow.ShowDialog(Form.ActiveForm); }));
                }
                else
                {
                    internetConnectionWindow.ShowDialog();
                }

                internetConnectionWindow.Dispose();
                internetConnectionWindow = null;
            }
        }

        #endregion
    }
}